<template>
  <div class="app-container">
        <router-link to="/ansible/exec">
            <el-button type="primary" icon="el-icon-plus" size="mini"  style="margin-bottom: 10px;">执行任务</el-button>
        </router-link>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="280px" align="center" label="AnsibleID/CeleryID">
        <template slot-scope="{row}">
          <span style="color:#1890ff"><router-link :to="'/ansible/task_detail/'+row.id">{{ row.AnsibleID }} </router-link></span><br>
          <span>{{ row.CeleryID }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column width="120px" align="center" label="CeleryID">
        <template slot-scope="{row}">
          <span>{{ row.CeleryID }}</span>
        </template>
      </el-table-column> -->

      <el-table-column width="100px" label="执行人">
        <template slot-scope="{row}">
          <span>{{ row.task_user }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" label="GroupName">
        <template slot-scope="{row}">
          <span>{{ row.GroupName }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" label="剧本文件">
        <template slot-scope="{row}">
          <span>{{ row.playbook }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" label="额外参数">
        <template slot-scope="{row}">
          <span>{{ row.ExtraVars }}</span>
        </template>
      </el-table-column>
      <el-table-column width="70px" label="状态">
        <template slot-scope="{row}">
          <span>{{ row.CeleryResult | celeryStatus }}</span>
        </template>
      </el-table-column>      
      <el-table-column width="100px" label="时间">
        <template slot-scope="{row}">
          <span> {{ row.CreateTime | substr }} </span>
        </template>
      </el-table-column>            
    </el-table>
  </div>
</template>

<script>
import { fetchList } from '@/api/ansibleui'

export default {
  name: 'InlineEditTable',
  filters: {
    celeryStatus(msg){
      if(msg == ""){
        return ""
      }
      return JSON.parse(msg).status
    },
    substr(msg){
      return msg.substr(0,10)
    },
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await fetchList('ansible_tasks', this.listQuery)
      const items = data.items
      this.list = items.map(v => {
        this.$set(v, 'edit', false) // https://vuejs.org/v2/guide/reactivity.html
        v.originalTitle = v.title //  will be used when user click the cancel botton
        return v
      })
      this.listLoading = false
    },
    cancelEdit(row) {
      row.title = row.originalTitle
      row.edit = false
      this.$message({
        message: 'The title has been restored to the original value',
        type: 'warning'
      })
    },
    confirmEdit(row) {
      row.edit = false
      row.originalTitle = row.title
      this.$message({
        message: 'The title has been edited',
        type: 'success'
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
